﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ScorchedTanks
{
    public class Actor3d
    {
        public Vector3 position;
        public Vector3 rotation;
        public Vector3 direction;
        public Model mModel;
        public float speed = 0;
 
        public bool readyToexit = false;
        public Actor3d()
        {
        }

        public Actor3d(ContentManager content, String model)
        {
            mModel = content.Load<Model>(model);
            direction = new Vector3(0, 0, 1);
            
        }

        public virtual void update(float dTime)
        {

        }

        public virtual void Draw(GraphicsDeviceManager g, Camera cam)
        {
            Matrix[] transforms = new Matrix[mModel.Bones.Count];
            mModel.CopyAbsoluteBoneTransformsTo(transforms);


            foreach (ModelMesh mesh in mModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateFromYawPitchRoll(rotation.X, rotation.Y, rotation.Z) * Matrix.CreateTranslation(position);
                    effect.View = Matrix.CreateLookAt(cam.GetPosition(), cam.GetLookAt(), Vector3.Up);
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), 800 / 600, 1.0f, 10000.0f);
                }
                mesh.Draw();
            }
        }

        public void Walk(float z)
        {
            position += direction * 5 * z;
        }

        public void Rotate(float x, float y, float z)
        {
            rotation.X += y;
            rotation.Y += x;
            rotation.Z += z;
            Vector3 originaldirection = new Vector3(0, 0, 1);
            direction = Vector3.Transform(originaldirection, Matrix.CreateFromYawPitchRoll(rotation.X, rotation.Y, rotation.Z));

        }

        public void Strafe(float x)
        {
            position += direction;
        }

        public void Fly(float z)
        {
            position.Y += z;
        }

        public void SetDirection(float xdir, float ydir, float zdir)
        {
            direction.X = xdir;
            direction.Y = ydir;
            direction.Z = zdir;
        }

        public void SetDirection(Vector3 dir)
        {
            direction.X = dir.X;
            direction.Y = dir.Y;
            direction.Z = dir.Z;
        }

        public void SetPosition(float xpos, float ypos, float zpos)
        {
            position.X = xpos;
            position.Y = ypos;
            position.Z = zpos;
        }

        public void SetPosition(Vector3 pos)
        {
            position.X = pos.X;
            position.Y = pos.Y;
            position.Z = pos.Z;
        }


        public Vector3 GetPosition()
        {
            return position;
        }

    }
}
